package com.panshi.controller;

import com.alibaba.fastjson.JSON;
import com.panshi.api.UserApi;
import com.panshi.utils.LoginVerifyShowPage;
import com.panshi.vo.ItemQueryUserVo;
import com.panshi.vo.R;
import com.panshi.vo.UserDetailResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@CrossOrigin
public class UserController {

    @Autowired
    UserApi userApi;

    //（个人）站内消息
    @RequestMapping("/messages")
    public ModelAndView messages(HttpSession httpSession){

        Integer userId = (Integer) httpSession.getAttribute(httpSession.getId());

        Map<String, Object> map = userApi.getUserInfo(userId.longValue()).getData();

        Object userMemberStr = map.get("userMemberInfo");
        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        ModelAndView modelAndView = new ModelAndView();
        //用户信息
        modelAndView.addObject("userMember",result.getUserMember());

        //登录后展示头部栏
        modelAndView.addObject("userObject",new LoginVerifyShowPage().showPage(userApi,httpSession));

        modelAndView.setViewName("messages");
        return modelAndView;
    }

    //（个人）站内消息
    @RequestMapping("/personalDylist")
    public ModelAndView personalDylist(Integer current, HttpSession session){

        Integer userId = (Integer) session.getAttribute(session.getId());

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(userId.longValue());
        itemQueryUserVo.setCurrent(current);

        Map<String, Object> map = userApi.msgList(itemQueryUserVo).getData();
        Object userMemberStr = map.get("userNoticeList");
        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        ModelAndView modelAndView = new ModelAndView();
        //用户站内消息数据
        modelAndView.addObject("userNoticeList",result.getUserNoticeList());

        modelAndView.setViewName("userNoticeMessage");
        return modelAndView;
    }

    //删除个人收藏
    @RequestMapping("/deleteUserCollect/{id}")
    @ResponseBody
    public R deleteUserCollect(@PathVariable("id") Long id){
        R r =  userApi.delCollect(id);
        return r;
    }

    //修改个人信息
    @RequestMapping("/updatePersonalDetail")
    @ResponseBody
    public R updatePersonalDetail(String nick,String userDesc,HttpSession session){
        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setMotto(userDesc);
        itemQueryUserVo.setNickname(nick);

        Integer userId = (Integer) session.getAttribute(session.getId());
        itemQueryUserVo.setId(userId.longValue());

        R r = userApi.updateUserInfo(itemQueryUserVo);
        return r;
    }

    //修改个人登录密码页面展示
    @RequestMapping("/personalSetPwd")
    public ModelAndView personalSetPwd(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("index",1);

        //修改的弹出框页面
        modelAndView.setViewName("/setEmail");
        return modelAndView;
    }

    //修改个人登录密码
    @RequestMapping("/personalupdatePwd")
    @ResponseBody
    public R personalupdatePwd(String pwd,String oldPwd,HttpSession session){
        System.out.println("修改密码:"+pwd+"---"+oldPwd);
        Integer userId = (Integer) session.getAttribute(session.getId());

        ModelAndView modelAndView = new ModelAndView();

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId((userId.longValue()));
        itemQueryUserVo.setPwd(pwd);
        itemQueryUserVo.setOldPwd(oldPwd);

        R r = userApi.updatePwd(itemQueryUserVo);
        return r;
    }

    //修改个人头像信息
    @RequestMapping("/uploadPersonHeaderBaseCode")
    @ResponseBody
    public R uploadPersonHeaderBaseCode(String upfile,HttpSession session){

        Integer userId = (Integer) session.getAttribute(session.getId());

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(userId.longValue());
        itemQueryUserVo.setFile(upfile);

        R r = userApi.updateUserHeadBaseCode(itemQueryUserVo);
        return r;
    }

    //修改个人邮箱登录帐号展示
    @RequestMapping("/personalSetEmail")
    public ModelAndView personalSetEmail(HttpSession session){

        Integer userId = (Integer) session.getAttribute(session.getId());

        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> map = userApi.getUserInfo(userId.longValue()).getData();
        Object userMemberStr = map.get("userMemberInfo");
        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        /*邮箱帐号*/
        String email =result.getUserMember().getEmail();
        if(StringUtils.isEmpty(email) || email==null){
            /*添加邮箱*/
            modelAndView.addObject("index",2);
        }else{
            /*已有邮箱（可发送验证码修改邮箱）*/
            modelAndView.addObject("email",email);
            modelAndView.addObject("index",0);
        }

        modelAndView.setViewName("/setEmail");
        return modelAndView;
    }

    //修改个人手机帐号展示
    @RequestMapping("/personalSetPhone")
    public ModelAndView personalSetPhone(HttpSession session){

        Integer userId = (Integer) session.getAttribute(session.getId());

        ModelAndView modelAndView = new ModelAndView();

        Map<String, Object> map = userApi.getUserInfo(userId.longValue()).getData();
        Object userMemberStr = map.get("userMemberInfo");
        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        /*手机号码*/
        String iphone =result.getUserMember().getPhone();

        if(StringUtils.isEmpty(iphone) || iphone==null){
            modelAndView.addObject("index",1);
        }else{
            modelAndView.addObject("phone",iphone);
            modelAndView.addObject("index",0);
        }

        modelAndView.setViewName("/setPhone");
        return modelAndView;
    }

    //个人信息
    @RequestMapping("/personalDetail")
    public ModelAndView personalDetail(HttpSession session){
        System.out.println("个人信息页！");
        Integer userId = (Integer) session.getAttribute(session.getId());

        ModelAndView modelAndView = new ModelAndView();

        Map<String, Object> map = userApi.getUserInfo(userId.longValue()).getData();
        Object userMemberStr = map.get("userMemberInfo");
        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        //用户信息
        modelAndView.addObject("userMember",result.getUserMember());

        //登录后展示头部栏
        modelAndView.addObject("userObject",new LoginVerifyShowPage().showPage(userApi,session));

        modelAndView.setViewName("personalDetail");
        return modelAndView;
    }

    //个人主页（动态）
    @RequestMapping("/personalHomepage")
    public ModelAndView personalHomepage(HttpSession session){

        ModelAndView modelAndView = new ModelAndView();

        Integer userId = (Integer) session.getAttribute(session.getId());

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(userId.longValue());

        Map<String, Object> map = userApi.userIndex(itemQueryUserVo).getData();
        Object userMemberStr = map.get("userMemberOne");
        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        //用户信息
        modelAndView.addObject("userMember",result.getUserMember());

        //提问，回答，文章数量
        modelAndView.addObject("mapData",result.getCountList());

        //收藏
        modelAndView.addObject("userCollectNum",result.getCollectNum());

        //登录后展示头部栏
        modelAndView.addObject("userObject",new LoginVerifyShowPage().showPage(userApi,session));

        modelAndView.setViewName("personalHomepage");
        return modelAndView;
    }

    //个人中心（动态）
    @RequestMapping("/personalCenterDynamicState")
    public ModelAndView dyList(Integer current,HttpSession session){
        ModelAndView modelAndView = new ModelAndView();

        Integer userId = (Integer) session.getAttribute(session.getId());

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(userId.longValue());
        itemQueryUserVo.setCurrent(current);

        Map<String, Object> map = userApi.dyList(itemQueryUserVo).getData();
        Object userMemberStr = map.get("userDynamicRelevanceList");
        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        //
        modelAndView.addObject("userDynamicRelevanceLists",result.getUserDynamicRelevanceList());

        //登录后展示头部栏
        modelAndView.addObject("userObject",new LoginVerifyShowPage().showPage(userApi,session));

        modelAndView.setViewName("userDynamicList");
        return modelAndView;
    }

    //个人中心（问题）
    @RequestMapping("/personalCenterQuiz")
    public ModelAndView qalist(Integer current,HttpSession session){
        ModelAndView modelAndView = new ModelAndView();

        Integer userId = (Integer) session.getAttribute(session.getId());

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(userId.longValue());
        itemQueryUserVo.setCurrent(current);

        Map<String, Object> map = userApi.qaList(itemQueryUserVo).getData();
        Object userMemberStr = map.get("qaQuestionList");
        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        //
        modelAndView.addObject("userDynamicRelevanceQuestionList",result.getUserDynamicQuestionList());

        modelAndView.setViewName("userDynamicList");

        return modelAndView;
    }

    //个人中心（回答）
    @RequestMapping("/personalCenterAnswer")
    public ModelAndView qaReplylist(Integer current,HttpSession session){
        ModelAndView modelAndView = new ModelAndView();

        Integer userId = (Integer) session.getAttribute(session.getId());

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(userId.longValue());
        itemQueryUserVo.setCurrent(current);

        Map<String, Object> map = userApi.answerList(itemQueryUserVo).getData();
        Object userMemberStr = map.get("qaAnswerList");
        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        modelAndView.addObject("userDynamicRelevanceAnswerList",result.getUserDynamicAnswerList());

        modelAndView.setViewName("userDynamicList");
        return modelAndView;
    }

    //个人中心（资讯）
    @RequestMapping("/personalCenterArticle")
    public ModelAndView articlelist(Integer current,HttpSession session){
        ModelAndView modelAndView = new ModelAndView();

        Integer userId = (Integer) session.getAttribute(session.getId());

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(userId.longValue());
        itemQueryUserVo.setCurrent(current);

        Map<String, Object> map = userApi.zxList(itemQueryUserVo).getData();
        Object userMemberStr = map.get("zxList");
        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        modelAndView.addObject("userDynamicRelevanceZxList",result.getUserDynamicZxList());

        modelAndView.setViewName("userDynamicList");
        return modelAndView;
    }

    //个人中心（收藏）
    @RequestMapping("/personalCenterCollect")
    public ModelAndView collectlist(Integer current,HttpSession session){
        ModelAndView modelAndView = new ModelAndView();

        Integer userId = (Integer) session.getAttribute(session.getId());

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(userId.longValue());
        itemQueryUserVo.setCurrent(current);

        Map<String, Object> map = userApi.collectList(itemQueryUserVo).getData();
        Object userMemberStr = map.get("userCollectList");
        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        modelAndView.addObject("userDynamicRelevanceCollectList",result.getUserCollectList());

        modelAndView.setViewName("userDynamicList");
        return modelAndView;
    }

    //修改个人站内消息状态
    @RequestMapping("/updatePersonalMessage")
    @ResponseBody
    public R updatePersonalMessage(Integer id){
        System.out.println("修改站内消息状态");
        R r = userApi.readNotice(id);
        return r;
    }

    //删除个人动态
    @RequestMapping("/deleteUserDynamic/{id}")
    @ResponseBody
    public R deleteUserDynamic(@PathVariable("id") Integer id){
        System.out.println("删除个人动态");
        R r = userApi.delDy(id);

        return r;
    }

    //发送邮箱验证码
    @RequestMapping("personalSendeUpdateEmail")
    @ResponseBody
    public R updateEmail(@RequestParam("userName")String userName, @RequestParam("type")String type, HttpSession session){
        System.out.println("发送验证码！");
        Integer userId = (Integer) session.getAttribute(session.getId());
        R r = userApi.sendEmail(userId,userName,type);
        return r;
    }

    //校验邮箱验证码是否正确
    @RequestMapping("personalCheckEmailCode")
    @ResponseBody
    public R checkEmailCode(@RequestParam("userName")String userName,@RequestParam("code")String code,@RequestParam("type")String type,HttpSession httpSession){
        System.out.println("校验验证码："+userName);
        Integer userId=(Integer)httpSession.getAttribute(httpSession.getId());
        R r = userApi.checkEmailCode(userId,userName,code,type);
        return r;
    }

    //发送短信验证码
    @RequestMapping("personalSendeUpdateIphone")
    @ResponseBody
    public R updateIphone(@RequestParam("userName")String userName,@RequestParam("type")String type,HttpSession session){
        System.out.println("发送短信验证");
        Integer userId = (Integer) session.getAttribute(session.getId());
        R r = userApi.sendIphone(userId,userName,type);
        return r;
    }

    //校验邮箱验证码是否正确
    @RequestMapping("personalCheckIphoneCode")
    @ResponseBody
    public R checkIphoneCode(@RequestParam("userName")String userName,@RequestParam("code")String code,@RequestParam("type")String type,HttpSession httpSession){
        System.out.println("校验验证码");
        Integer userId=(Integer)httpSession.getAttribute(httpSession.getId());
        R r = userApi.checkIphoneCode(userId,userName,code,type);
        return r;
    }
}
